package com.xianzhiOA.service.impl;

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzProject;
import com.xianzhiOA.dao.XzEmpMapper;
import com.xianzhiOA.dao.XzProjectMapper;
import com.xianzhiOA.service.XzProjectService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *ProjectServiceImpl
 *
 * @author 甘汝雷
 * @create 2017-04-19 10:17
 **/
@Service("xzProjectService")
public  class XzProjectServiceImpl implements XzProjectService {
    @Autowired
    private XzProjectMapper projectMapper;
    @Autowired
    private XzEmpMapper empMapper;

    //添加项目
    @Override
    public int insert(XzProject project) {
        return projectMapper.insert(project);
    }
    //分页查询
    @Override
    public PageBean<XzProject> selAll(XzProject project, int page, int rows, String beginDate, String endDate) {
        PageHelper.startPage(page, rows);
        Map<String, Object> map = myMap(project);
        if (beginDate != null) map.put("beginDate", myDate(beginDate));
        if (endDate != null) map.put("endDate", myDate(endDate));
        List<XzProject> plist = projectMapper.selAll(map);
        PageBean<XzProject> pb = new PageBean<>(plist);
        List<XzProject> pbList = pb.getList();
        for (XzProject p: pb.getList()) {
            if (p.getProjectOfficial() != null&& p.getProjectOfficial()!=0) {
               XzEmp emp = empMapper.selectByPrimaryKey(p.getProjectOfficial());
                p.setOfficial(emp.getEmpName());
            }
            if (p.getProjectAudit()!=null&&p.getProjectAudit() != 0){
                XzEmp emp = empMapper.selectByPrimaryKey(p.getProjectAudit());
                p.setAudit(emp.getEmpName());
            }
        }
        return pb;
    }
    //查询所有
    @Override
    public List<XzProject> selAll(XzProject project){
        return projectMapper.selAll(myMap(project));
    }

    //把project转换成Map类型
    public Map myMap(XzProject project){
        Map map = new HashMap();
        map.put("projectId",project.getProjectId());
        map.put("projectName",project.getProjectName());
        map.put("official",project.getOfficial());
        map.put("audit",project.getAudit());
        map.put("projectOfficial",project.getProjectOfficial());
        map.put("projectAudit",project.getProjectAudit());
        map.put("projectDescribe",project.getProjectDescribe());
        return map;
    }
    //通过id删除项目
    @Override
    public int delete(Long projectId) {
        return projectMapper.delete(projectId);
    }
    //伪删除
    @Override
    public int isdelete(Long projectId) {
        Map map = new HashMap();
        map.put("projectId",projectId);
        map.put("isdelete",1);
        return projectMapper.update(map);
    }

    //通过项目名查询项目
    @Override
    public XzProject selByName(String projectName) {
        return projectMapper.selByName(projectName);
    }

    //修改项目
    @Override
    public int update(XzProject project) {
        return projectMapper.update(myMap(project));
    }

    //通过id查询项目
    @Override
    public XzProject selById(Long projectId) {
        return projectMapper.selById(projectId);
    }
    @Override
    public Date myDate(String date){
        SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = fmt.parse(date);
        }catch (ParseException e){

        }
        return parse;
    }

}
